﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WAD.WEB.AppCode;
using QD.Utils;
using WAD.DAL;

namespace WAD.WEB.Passport.ajax
{
    /// <summary>
    /// OAuth 的摘要说明
    /// </summary>
    public class OAuth : PassportAjax
    {
        public override void AjaxMethodDispatch(String identity)
        {
            switch (identity)
            {
                case "agent_redirect": this.Agent_Redirect(); break;
            }
        }
        /// <summary>
        /// 商汇通授权登录微街秀
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        void Agent_Redirect()
        {
            int userid = Parse.Int("userid", 0);
            string time = Parse.String("time", "");
            string name = Parse.String("name", "");
            string url = Parse.String("url", "");
            if (userid < 1 || name.Equals(""))
                ParamError();

            string sign = Parse.String("sign", "");     //sign=md5((shtuserid-100000)+time.substring(5))
            if (!CheckSign(userid, name, time, sign))
                return;

            //根据商汇通ID获取微街秀ID
            int id = GetUserId(userid);
            Dictionary<string, object> m = DaoUser.GetDetail(id, "id,name,login_type,parent_id,token");
            if (m == null)
            {
                Response.Write("代理商不存在");
                return;
            }

            SetLogin(m);
            if (!string.IsNullOrEmpty(url))
                Response.Redirect(url);
            Response.Redirect("index.aspx");
        }

        //签名验证
        bool CheckSign(int userid, string name, string time, string sign)
        {
            //if (time.Length < 5)
            //{
            //    Response.Write("无效的时间戳");
            //    return false;
            //}
            //string checkSign = Encrypt.MD5(Convert.ToString(userid - 100000) + time.Substring(5));
            string checkSign = Encrypt.MD5(Convert.ToString(userid - 100000));
            if (sign != checkSign)
            {
                Response.Write("签名无效");
                Response.Write("<br>sign1: " + sign);
                Response.Write("<br>sign2: " + checkSign);
            }
            return true;
        }

        //根据 商汇通ID 获取 微街秀ID
        int GetUserId(int userid)
        {
            int id = 0;
            try
            {
                Dictionary<string, object> dic = WAD.DAL.Union.DAOAgent.Get("vifo_users_agent", "userid=" + userid, "userid_wjx");
                if (dic != null && dic["userid_wjx"] != DBNull.Value)
                    id = Convert.ToInt32(dic["userid_wjx"]);
            }
            catch { }
            return id;
        }

        void SetLogin(Dictionary<string, object> dic)
        {
            Response.Cookies.Add(new HttpCookie("userid", Convert.ToString(dic["id"])));
            Response.Cookies.Add(new HttpCookie("name", Convert.ToString(dic["name"])));
            Response.Cookies.Add(new HttpCookie("logintype", Convert.ToString(dic["login_type"])));
            Response.Cookies.Add(new HttpCookie("parentid", Convert.ToString(dic["parent_id"])));
            Response.Cookies.Add(new HttpCookie("token", Convert.ToString(dic["token"])));
        }
    }
}